package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonWithPay;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2017/3/25.
 */

public class NewPayActivity extends BaseActivity implements View.OnClickListener {

    //false为充值

    private Button bt_01;
    private PopupWindow popWindow;
    private EditText et_01;

    private String money, msg;
    private TextView tv_title_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newpay);
        bt_01 = (Button) findViewById(R.id.bt_01);
        bt_01.setOnClickListener(this);
        et_01 = (EditText) findViewById(R.id.et_01);
        tv_title_right = (TextView) findViewById(R.id.tv_title_right);
        tv_title_right.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {

            case R.id.bt_01:

                showPopupWindow(bt_01);
                break;

            case R.id.tv_title_right:

                intent.putExtra("mode", false);//true为提现，false为充值
                intent.setClass(this, WithdrawOrRechargeRecordActivity.class);
                startActivity(intent);
                break;

        }
    }

    private void showPopupWindow(View parent) {
        if (popWindow == null) {
            View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_03, null)));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void initPop(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册

        TextView text_moneycount = (TextView) view.findViewById(R.id.text_moneycount);


        text_moneycount.setText(et_01.getText().toString());


        final LinearLayout linear_dialog3 = (LinearLayout) view.findViewById(R.id.linear_dialog3);

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                popWindow.dismiss();
                popWindow = null;

            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                money = et_01.getText().toString();
                //getRechargeAccessToken(money);
                getRechargeAccessToken(money);

            }
        });
    }

    private void getRechargeAccessToken(final String money) {
        new JsonAccessToken("pay/charge", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        // String url = "https://www.haiying365.com/api/pay/charge?uid=" + MyApplication.myPreferences.readUid() + "&money=" + money + "&way=ecpss2" + "&access_token=" + info.access_token;
                        //https://open.fuiou.com/gateway/appDocList.do
                        initWithPayvalidate(info.access_token);

                        //  String url = " http://www-1.fuiou.com:18670/mobile_pay/update/receiveSDK.pay?uid=" + MyApplication.myPreferences.readUid() + "&money=" + money + "&way=ecpss2" + "&access_token=" + info.access_token;
                      /* String url = "https://open.fuiou.com/gateway/appDocList.do/api/pay/charge?uid=" + MyApplication.myPreferences.readUid() + "&money=" + money + "&way=ecpss2" + "&access_token=" + info.access_token;
                        wv01.setVisibility(View.VISIBLE);
                        wv01.loadUrl(url);*/

                    } else {
                        UtilToast.show(NewPayActivity.this, "安全验证失败！");
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(NewPayActivity.this, "安全验证失败！");
            }
        }).execute(this, true);

    }

    private void initWithPayvalidate(final String access_token) {


        new JsonWithPay(access_token, MyApplication.myPreferences.readUid(), et_01.getText().toString(), "500002", new AsyCallBack<JsonWithPay.Info>() {

            @Override
            public void onSuccess(String toast, int type, final JsonWithPay.Info info) throws Exception {
                if (info != null) {
                    String string = info.url;
                    if (!TextUtils.isEmpty(string)) {
                        Intent intent = new Intent();
                        intent.putExtra("url", string);
                        intent.setClass(NewPayActivity.this, PayWebActivity.class);
                        startActivity(intent);

                    }

                    popWindow.dismiss();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                bt_01.setSelected(false);
                msg = JsonWithPay.TOAST;
                if (!TextUtils.isEmpty(msg)) {
                    //   showPopupWindow1(bt01);
                    Toast.makeText(NewPayActivity.this, msg, Toast.LENGTH_SHORT).show();
                    popWindow.dismiss();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                //   Toast.makeText(WithdrawOrRechargeActivity.this, getRequest, Toast.LENGTH_SHORT).show();
                popWindow.dismiss();
            }
        }).execute(this, true);


    }
}
